package com.myw.mail.controller;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.myw.mail.domain.vo.CategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.myw.mail.domain.Category;
import com.myw.mail.service.ICategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 分类Controller
 * 
 * @author myw
 * @date 2025-07-10
 */
@RestController
@RequestMapping("/mail/category")
public class CategoryController extends BaseController
{
    @Autowired
    private ICategoryService categoryService;

    /**
     * 查询分类列表
     */
    @PreAuthorize("@ss.hasPermi('mail:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(Category category)
    {
        startPage();
        List<Category> list = categoryService.selectCategoryList(category);
        Map<Long, Category> map = list.stream()
                .collect(Collectors.toMap(Category::getId, Function.identity()));
        List<CategoryVo> listVo = list.stream().map(item ->{
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(item, categoryVo);
            if (item.getParentId() != null && item.getParentId() != 0) {
                Category parent = map.get(item.getParentId());
                categoryVo.setParentName(parent != null ? parent.getCategoryName() : "");
            } else {
                categoryVo.setParentName("");
            }
            return categoryVo;
        }).collect(Collectors.toList());
        return getDataTable(listVo);
    }

    /**
     * 导出分类列表
     */
    @PreAuthorize("@ss.hasPermi('mail:category:export')")
    @Log(title = "分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Category category)
    {
        List<Category> list = categoryService.selectCategoryList(category);
        Map<Long, Category> map = list.stream()
                .collect(Collectors.toMap(Category::getId, Function.identity()));
        List<CategoryVo> listVo = list.stream().map(item ->{
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(item, categoryVo);
            if (item.getParentId() != null && item.getParentId() != 0) {
                Category parent = map.get(item.getParentId());
                categoryVo.setParentName(parent != null ? parent.getCategoryName() : "");
            } else {
                categoryVo.setParentName("");
            }
            return categoryVo;
        }).collect(Collectors.toList());
        ExcelUtil<CategoryVo> util = new ExcelUtil<CategoryVo>(CategoryVo.class);
        util.exportExcel(response, listVo, "分类数据");
    }

    /**
     * 获取分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('mail:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(categoryService.selectCategoryById(id));
    }

    /**
     * 新增分类
     */
    @PreAuthorize("@ss.hasPermi('mail:category:add')")
    @PostMapping
    public AjaxResult add(@RequestBody Category category)
    {
        return toAjax(categoryService.insertCategory(category));
    }

    /**
     * 修改分类
     */
    @PreAuthorize("@ss.hasPermi('mail:category:edit')")
    @Log(title = "分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Category category)
    {
        return toAjax(categoryService.updateCategory(category));
    }

    /**
     * 删除分类
     */
    @PreAuthorize("@ss.hasPermi('mail:category:remove')")
    @Log(title = "分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(categoryService.deleteCategoryByIds(ids));
    }


    /**
     * 获取父类分类列表
     */
    @GetMapping("/getCategoryList")
    public AjaxResult getCategoryParentList(){
        return success(categoryService.getCategoryParentList());
    }

}
